import java.util.Random;

abstract class Animal {
	protected int borderX = 0;
	protected int borderY = 0;
	private int level=0;
	private int lifetime=0;
	protected int moveX = 0;
	protected int moveY = 0;
	protected boolean survival = true;
	protected String name;
	protected final int[][] direction = { { 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 }, { -1, 0 }, { -1, -1 }, { -1, 0 },
			{ -1, 1 } };

	Animal(int X, int Y, String NAME,int LV) {
		borderX = X;
		borderY = Y;
		name = NAME;
		level=LV;
	}

	public void move() {
		

		
		if(!getSurvival()) {
			System.out.println(name+" is dead .");
			return ;
		}
		Random r = new Random();
		//int moveX;
		//int moveY;

		//moveX = r.nextInt(borderX);
		//moveY = r.nextInt(borderY);

		//if(!getSurvival())

		int nextX;
		int nextY;
		int rdirection = r.nextInt(8);
		nextX = moveX + direction[rdirection][0];
		nextY = moveY + direction[rdirection][1];
		while (nextX > borderX || nextY > borderY || nextX < 0 || nextY < 0) {

			System.out.println(name + " beyond the border ,try again.");
			rdirection = r.nextInt(8);
			nextX = moveX + direction[rdirection][0];
			nextY = moveY + direction[rdirection][1];
		}
		moveX = nextX;
		moveY = nextY;

		System.out.println("The " + ++lifetime +" time "+name + "moving:(" + moveX + "," + moveY + ")");
		

	}

	protected int getX() {
		return moveX;
	}

	protected int getY() {
		return moveY;
	}

	protected void setSurvival() {
		survival = false;
	}

	protected boolean getSurvival() {
		return survival;
	}
	
	private void rank(Animal a[]) {
		
	}
	
	protected int getLv() {
		return level;
	}
	public void eat(Animal a[]) {
		rank(a);
		
		
		for(int i=a.length-1;i>0;i--) {
			for(int j=i-1;j>=0;j--) {
				if ((a[i].getSurvival() && a[j].getSurvival()) && a[i].getX() == a[j].getX() && a[i].getY() == a[j].getY()) {
					if(a[i].getLv()>a[j].getLv()) {
						a[j].setSurvival();
					}
					else if(a[i].getLv()<a[j].getLv()){
						a[i].setSurvival();
						
					}
				}
			}
		}
		/*if ((a[2].getSurvival() && a[1].getSurvival()) && a[2].getX() == a[1].getX() && a[2].getY() == a[1].getY()) {
			a[1].setSurvival();
		}
		if (a[0].getSurvival() && a[2].getX() == a[0].getX() && a[2].getY() == a[0].getY()) {
			a[0].setSurvival();
		}
		if ((a[1].getSurvival() && a[0].getSurvival()) && (a[1].getX() == a[0].getX() && a[1].getY() == a[0].getY())) {
			a[0].setSurvival();
		}*/
	}
}
